<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * This is a sample module for PyroCMS
 *
 * @author 		Jerel Unruh - PyroCMS Dev Team
 * @website		http://unruhdesigns.com
 * @package 	PyroCMS
 * @subpackage 	Sample Module
 */
class Admin extends Admin_Controller
{
	protected $section = 'log_action';
	protected $_ci;
	public function __construct()
	{
		parent::__construct();
		$this->_ci = &get_instance();
		// Load all the required classes
		$this->lang->load('log_action');

		$this->template->set_partial('shortcuts', 'admin/partials/shortcuts')
		->append_metadata(js('escort.log.action.js', 'be_log_action'))
		->append_metadata(css('escort.log.action.css', 'be_log_action'));
	}

	/**
	 * List all items
	 */
	public function index()
	{
		// Create pagination links
		$total_rows = $this->_ci->action_log->countLogActionList();
		$pagination = create_pagination('admin/be_log_action/index', $total_rows);
		// Using this data, get the relevant results
		$logActions = $this->_ci->action_log->getLogActionList('',$pagination['limit']);

		//do we need to unset the layout because the request is ajax?
		$this->input->is_ajax_request() ? $this->template->set_layout(FALSE) : '';

		$this->template
		->title($this->module_details['name'])
		->append_metadata(js('admin/filter.js'))
		->set('pagination', $pagination)
		->set('logActions', $logActions);

		$this->input->is_ajax_request() ? $this->template->build('admin/tables/posts', $this->data) : $this->template->build('admin/index', $this->data);
			
	}
	public function delete($id = 0)
	{
		// Delete one
		$ids = ($id) ? array($id) : $this->input->post('action_to');
		// Go through the array of slugs to delete
		if ( ! empty($ids))
		{
			$logActionIds = array();
			foreach ($ids as $id)
			{
				// Get the current page so we can grab the id too
				if ($logAction = $this->_ci->action_log->init('id',$id))
				{
					$this->_ci->action_log->delete($id);

					// Wipe cache for this model, the content has changed
					$logActionIds[] = $logAction->id;
				}
			}
		}

		// Some pages have been deleted
		if ( ! empty($logActionIds))
		{
			// Only deleting one page
			if (count($logActionIds) == 1)
			{
				$this->session->set_flashdata('success', sprintf($this->lang->line('escort_log_action_delete_success'), count($logActionIds[0])));
			}
			// Deleting multiple pages
			else
			{
				$this->session->set_flashdata('success', sprintf($this->lang->line('escort_log_action_mass_delete_success'), count($logActionIds)));
			}
		}
		// For some reason, none of them were deleted
		else
		{
			$this->session->set_flashdata('notice', lang('escort_log_action_delete_error'));
		}

		redirect('admin/be_log_action');
	}
	public function action()
	{
		switch ($this->input->post('btnAction'))
		{
			case 'delete':
				$this->delete();
				break;
			
			default:
				redirect('admin/be_log_action');
				break;
		}
	}
}
